/*
	The MIT License

	Copyright (c) 2010 IFMO/GameDev Studio

	Permission is hereby granted, free of charge, to any person obtaining a copy
	of this software and associated documentation files (the "Software"), to deal
	in the Software without restriction, including without limitation the rights
	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
	copies of the Software, and to permit persons to whom the Software is
	furnished to do so, subject to the following conditions:

	The above copyright notice and this permission notice shall be included in
	all copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
	THE SOFTWARE.
*/

#include "math.h"

/*-----------------------------------------------------------------------------
	Frustum :
-----------------------------------------------------------------------------*/

EFrustum::EFrustum( void )
{
	SetFrustum(1,1,0,1);
	SetPosition( EPoint() );
	SetOrient( EQuaternion() );
}


EFrustum::~EFrustum( void )
{

}


void EFrustum::SetFrustum( float w, float h, float zn, float zf )
{
	this->w		=	w;
	this->h		=	h;
	this->zf	=	zf;
	this->zn	=	zn;
	Recompute();
}


void EFrustum::SetPosition( const EPoint &pos )
{
	this->pos	=	pos;
	Recompute();
}


void EFrustum::SetOrient( const EQuaternion &q )
{
	this->orient	=	q;
	Recompute();
}


/*-----------------------------------------------------------------------------
	Culling stuff :
-----------------------------------------------------------------------------*/

void EFrustum::getCorners( EPoint &top, EPoint &tr, EPoint &tl, EPoint &br, EPoint &bl, bool transform/*=true */ ) const
{
	top	=	EPoint(0, 0, 0);
	tr	=	EPoint(  w/2.0f, h/2.0f, -zf );
	tl	=	EPoint( -w/2.0f, h/2.0f, -zf );
	br	=	EPoint(  w/2.0f,-h/2.0f, -zf );
	bl	=	EPoint( -w/2.0f,-h/2.0f, -zf );
	
	if (transform) {
		EMatrix	T	=	orient.ToMatrix() * EMatrix::Translate(EVector(pos));
		top	=	top.Transform( T );	
		tr	=	tr.Transform ( T );	
		tl	=	tl.Transform ( T );	
		br	=	br.Transform ( T );	
		bl	=	bl.Transform ( T );	
	}
}

//
//	EFrustum::Recompute
//	frustum directed along local X-axis
//
void EFrustum::Recompute( void )
{
	EPoint	top, tr, tl, br, bl;
	getCorners( top, tr, tl, br, bl, true );

	// CCW winding :
	this->right		=	PlaneFromPoints( top, br, tr );	
	this->left		=	PlaneFromPoints( top, tl, bl );
	this->top		=	PlaneFromPoints( top, tr, tl );
	this->bottom	=	PlaneFromPoints( top, bl, br );
	this->far_plane	=	PlaneFromPoints( tr, bl, tl );
	this->near_plane=	far_plane;
	float range			=	zf - zn;
	near_plane.d += range;
	near_plane.a *= -1;
	near_plane.b *= -1;
	near_plane.c *= -1;
	near_plane.d *= -1;
}


//
//	EFrustum::IsPointInside
//
bool EFrustum::IsPointInside( const EPoint &point, float margin /* = 0 */ ) const
{
	if ( PlaneDistance( right		, point ) > margin ) 	return false;
	if ( PlaneDistance( left		, point ) > margin ) 	return false;
	if ( PlaneDistance( top			, point ) > margin ) 	return false;
	if ( PlaneDistance( bottom		, point ) > margin ) 	return false;
	if ( PlaneDistance( far_plane	, point ) > margin ) 	return false;
	if ( PlaneDistance( near_plane	, point ) > margin ) 	return false;
	return true;
}


//
//	EFrustum::IsSphereInside
//
bool EFrustum::IsSphereInside( const ESphere &sphere ) const
{
	EPoint	center	=	sphere.GetPosition();
	float	radius	=	sphere.GetRadius();

	if ( PlaneDistance( right		, center ) > radius) 	return false;
	if ( PlaneDistance( left		, center ) > radius) 	return false;
	if ( PlaneDistance( top			, center ) > radius) 	return false;
	if ( PlaneDistance( bottom		, center ) > radius) 	return false;
	if ( PlaneDistance( far_plane	, center ) > radius) 	return false;
	if ( PlaneDistance( near_plane	, center ) > radius) 	return false;
	return true;
}


//
//	EFrustum::GetCircumSphere
//	NOTE: sphere is circumscribed around triangle laying on far plane diagonal and origin of the frustum
//	see: wiki
//
ESphere EFrustum::GetCircumSphere( void ) const
{
	EPoint	top, tr, tl, br, bl;
	getCorners( top, tr, tl, br, bl, true );

	EVector	dir	=	((tr - top) + (tl - top)) * 0.5f;
	float	h	=	dir.Length();
	
	dir.NormalizeSelf();
	
	float	a	=	( tr - top ).Length();
	float	b	=	a;
	float	c	=	( tr - tl ).Length();
	
	float	S	=	0.5f * h * c;
	float	R	=	a*b*c / 4 / S;
	
	EPoint	center	=	top + dir * R;

	return ESphere(center, R);
}


//
//	EFrustum::GetEyeRay
//
EVector EFrustum::GetEyeRay( float x, float y ) const
{
	float u = x*0.5f + 0.5f;
	float v = y*0.5f + 0.5f;

	EPoint	top, tr, tl, br, bl;
	getCorners( top, tr, tl, br, bl, true );
	
	EPoint	p0	=	EMath::Lerp( bl, br, u );
	EPoint	p1	=	EMath::Lerp( tl, tr, u );
	EPoint	p	=	EMath::Lerp( p0, p1, v );
	
	EVector	r	=	EVector(p);
	
	r	=	r.Rotate( orient );
	
	return r;
}